<?php
namespace Orx\Mailer\MailBox\Message\Reader;
/**
 * Message Default Html Renderer
 * 
 * 
 */
class Classic extends \Fox\Html\Object {
		
	/**
	 * Mail Message
	 * @var \Orx\Mailer\MailBox\Message
	 */
	protected $message = null;
	
	/**
	 * Collapse ?
	 * @bool
	 */
	public $collapsed = true;
	
	/**
	 * Display Title ?
	 * @bool
	 */
	public $withTitle = false;
		
	/**
	 * Generator for header label
	 * @var Fox\Generator
	 */
	public $labelGenerator = null;
	
	/**
	 * Generator for title
	 * @var Fox\Generator
	 */
	public $titleGenerator = null;
	
	/**
	 * Constructor
	 * @param \Orx\Mailer\MailBox\Message $message
	 */
	public function __construct(\Orx\Mailer\MailBox\Message $message = null,$withTitle = false,$collapsed = true,$labelGenerator = null) {
		$this->message = $message;
		$this->collapsed = $collapsed;
		$this->withTitle = $withTitle;
		$this->labelGenerator = $labelGenerator;
		parent::__construct('message-view');
	}
	
	/**
	 * Overwrite Label Generator
	 * @param \Fox\Generator $generator
	 * @return Orx\Mailer\MailBox\Message\Reader\Classic
	 */
	public function labelGenerator($generator) {
		$this->labelGenerator = $generator;
		return $this;
	}
	
	/**
	 * Overwrite Label Generator
	 * @param \Fox\Generator $generator
	 * @return Orx\Mailer\MailBox\Message\Reader\Classic
	 */
	public function titleGenerator($generator) {
		$this->titleGenerator = $generator;
		return $this;
	}	
	/**
	 * 
	 * Output the message has html 
	 * @param \Orx\Mailer\MailBox\Message $message
	 */
	public function render() {
		$message = $this->message;
		// Dertmine if what kind of contact it is
		$data = $message->getData();
		$title = $data['subject'];
		$authorPictureUrl = _IMGDIR_.'profile_mask.png';
		if(!empty($this->labelGenerator)) {
			$author = \Fox\Generator::generateValue($this->labelGenerator, $data);
		} else {
			$name = !empty($data['name']) ?  $data['name'] : $data['email'];
			$author = $data['name'].'<span class="email">&lt;'.$data['email'].'&gt;</span>';
		}
		
		$content = !empty($data['content_html']) ?$data['content_html']:$data['content'] ;
		$receivedDate = \Fox\Utilities\DateTimes::toFullString($data['received_date']);
		$content = \Fox\Utilities\Text::closeTags($content);
		// Parse Images 
		if(preg_match_all("/<img(.*)src=\s*([\"][^\"]+[\"]|[^>]+)/", $content, $matches)) {
			if(isset($matches[2])) {
				foreach($matches[2] as $match) {
					foreach($message->attachements() as $attachement) {
						$filename = $attachement->filename;
						if(preg_match("/".$filename."/",$match)) {
							// Get File name extension
							$parts = explode('.',$filename);
							
							if(count($parts) > 1) {
								$extension = strtolower($parts[count($parts)-1]);
								$source = 'data:image/'.$extension.';charset=utf-8;base64,';
								$source .= $attachement->content;
								$content = str_replace($match,'"'.$source.'"',$content);
							}
							break;
						}
					}
				}
			}
		}
		
		$collapsed = ($this->collapsed) ? 'collapsed' : '';
		// Html
		$html ='<div class="mailbox-message '.$collapsed.'">';
		if($this->withTitle) {
			$additionnalTitle = null;
			if(!empty($this->titleGenerator)) {
				$additionnalTitle = \Fox\Generator::generateValue($this->titleGenerator, $message);
			}
			$html .='<h1 class="message-title">'.$title.$additionnalTitle.'</h1>';
		}
		$recipients = null;
		foreach($message->receivers() as $receiver) {
			if(!empty($this->labelGenerator)) {
				$label = \Fox\Generator::generateValue($this->labelGenerator, $receiver->getData());
			} else {
				$label = $receiver->email;
			}
			$recipients .= empty($recipients) ? $label : ','.$label;
		}
		$recipientMessages = Msg(\Orx\Mailer\Messages::BUNDLE,
								\Orx\Mailer\Messages::MESSAGE_TO_RECIPIENT_LIST,
								$recipients);
		$summary = $message->content_html;
		$summary = $message::summary($summary);
		
		// Attachements
		$attachements = $message->attachements();
		$attachementIcon = $htmlFooter = null;
		if(count($attachements) > 0) {
			$inlineAttachements = null;
			$htmlFooter = '<ul class="attachements">';
			foreach($attachements as $attachement) {
				$inlineAttachements .= empty($inlineAttachements) ? $attachement->filename : ','.$attachement->filename;
				$htmlFooter .= '<li><i class="'.$attachement->type().'"></i><span class="filename">'.$attachement->filename.'</span>';
				$htmlFooter .= '<br><a target="_blank" href="?mod=messages&act=attachements&sac=download&xvtrs=none&inlineid='.$attachement->getInLineId().'">';
				$htmlFooter .= Msg(\Orx\Mailer\Messages::BUNDLE,\Orx\Mailer\Messages::DOWNLOAD);
				$htmlFooter .= '</a>';
				$htmlFooter .= '</li>';
			}
			$htmlFooter .= '</ul>';
			$attachementIcon = '<div style="margin-right:5px;line-height: 30px;float: right;"><i class="icon icon-file" title="'.$inlineAttachements.'"></i></div>';
		}
		$html .= '<div class="message-header clickable ">
					<img src="'.$authorPictureUrl.'" class="author-picture">
					<div class="message-author">'.$author.' 
						<span class="recipient">'.$recipientMessages.'</span>
					</div>
					<div class="message-summary">'.$summary.'</div>
					<div class="received-date">'.$receivedDate.'</div>
					'.$attachementIcon.'
				</div>
				<div class="message-content">
				'.$content;
		$html .= $htmlFooter;
		$html .= '</div>';
		
		$html .= '</div>';
		return $html;
	}
}